package com.therandoms.rtgdroid.background;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

import android.os.Environment;

/**
 * A simple set of functions to copy files in Android
 * @author Logan Widick
 */
public class FileCopier {
	/**
	 * Copies a file descriptor to a file on Android.
	 * 
	 * @param src
	 * @param dst
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	public static void copyFdToFile(FileDescriptor src, File dst)
			throws IOException {
		FileChannel inChannel = new FileInputStream(src).getChannel();
		FileChannel outChannel = new FileOutputStream(dst).getChannel();
		try {
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} finally {
			if (inChannel != null)
				inChannel.close();
			if (outChannel != null)
				outChannel.close();
		}
	}

	/**
	 * Copies an input stream to a file on Android.
	 * 
	 * @param src the input stream
	 * @param dst the file
	 * @throws IOException
	 */
	public static void copyInputStreamToFile(InputStream src, File dst) throws IOException {
		FileOutputStream out = new FileOutputStream(dst);
		byte[] buffer = new byte[1024];
		int len;
		while ((len = src.read(buffer)) != -1) {
		    out.write(buffer, 0, len);
		}
		out.close();
	}

	/**
	 * Copies a resource to a file.
	 * 
	 * @param src
	 *            The resource to use.
	 * @param subdir
	 *            the directory in external storage to put the file in.
	 * @param fileName
	 *            the name of the file.
	 * @return the path to the file on external storage.
	 */
	public static String copyResourceToFile(InputStream src, String subdir,
			String fileName) {
		File extDir = Environment.getExternalStorageDirectory();
		File grammarDir = new File(extDir.getAbsolutePath()
				+ File.separator + subdir);
		if (!grammarDir.isDirectory() && !grammarDir.mkdirs()) {
			System.out.println("GrammarDir " + grammarDir.getAbsolutePath() + " is not working");
			return null;
		} else {
			File mFile = new File(grammarDir.getAbsolutePath()
					+ File.separator + fileName);
			try {
				if (!mFile.isFile() && !mFile.createNewFile()) {
					System.out.println("mFile " + mFile.getAbsolutePath() + " is not working");
					return null;
				} else {
					copyInputStreamToFile(src, mFile);
					return mFile.getAbsolutePath();
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}
	}

	/**
	 * Copies a resource to a file.
	 * 
	 * @param src
	 *            The resource to use.
	 * @param subdir
	 *            the directory in external storage to put the file in.
	 * @param fileName
	 *            the name of the file.
	 * @return the path to the file on external storage.
	 */
	public static String copyResourceToFile(FileDescriptor src, String subdir,
			String fileName) {
		File extDir = Environment.getExternalStorageDirectory();
		File grammarDir = new File(extDir.getAbsolutePath()
				+ File.separator + subdir);
		if (!grammarDir.isDirectory() && !grammarDir.mkdirs()) {
			return null;
		} else {
			File mFile = new File(grammarDir.getAbsolutePath()
					+ File.separator + fileName);
			try {
				if (!mFile.isFile() && !mFile.createNewFile()) {
					return null;
				} else {
					copyFdToFile(src, mFile);
					return mFile.getAbsolutePath();
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}
	}
}
